package com.example.resourceSharingPlatform.service;

import com.example.resourceSharingPlatform.config.security.jwt.JwtTokenUtils;
import com.example.resourceSharingPlatform.dao.CDKeyDao;
import com.example.resourceSharingPlatform.entity.Account;
import com.example.resourceSharingPlatform.entity.CDKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CdkService {

    @Autowired
    private CDKeyDao cdkDao;

    @Autowired
    private AccountService accountService;

    public Boolean RechargeByCDK(String key,String token){



        CDKey cdk=cdkDao.queryCDKeyByKey(key);
        double amount = cdk.getAmount();
        String used = cdk.getUsed();
        if(used.equals("1")){
            return false;
        }
        else if(used.equals("0")){
            String owner_id = new JwtTokenUtils().getUsernameFromToken(token);
            Account account = accountService.queryAccountById(owner_id);
            account.setBalance(account.getBalance()+amount);
            accountService.updateAccount(account);
            return cdkDao.updateCDKeyByKey(key,"1");
        }

        return false;

   }
}
